﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;


namespace LCWMS
{
    public partial class w_so012 : DockContent
    {
        public w_so012()
        {
            InitializeComponent();
        }
        private DataSet dscustomer;

        #region  自定义函数区

        private void f_displayreport(string str_consignor, string str_sono)
        {  
            DataSet ds_tmp;
            string str_temp;

            //得到拣货明细信息

            try
            {
                if (main.dbtype == "SQL")
                {   
                    str_temp = "select case (select count(*) from " + main.g_space + ".so_pick_detail q where q.inv_org=a.inv_org and q.consignor=a.consignor and q.so_no=a.so_no) when 0 then '全部缺料' else '部分缺料' end short_dist, b.order_unit,a.consignor,a.so_no,a.so_day,c.so_line_no,c.wave_no,c.sku,d.sku_name,c.plan_so_qty_m,c.plan_so_qty_s" +
                               " ,c.short_qty_m,c.short_qty_s,a.customer_sale_no work_no,b.lot_attribute3 tax_dist,d.spec,d.type,e.work_line,b.lot_attribute5,d.warehouseman " +
                               " from " + main.g_space + ".so_ship_header a," + main.g_space + ".so_ship_detail b," + main.g_space + ".so_pick_short c," + main.g_space + ".mst_sku d," + main.g_space + ".so_work_no e where a.inv_org=b.inv_org and " +
                               " b.inv_org=c.inv_org and c.inv_org=d.inv_org and a.consignor=b.consignor and b.consignor=c.consignor and c.consignor=d.consignor " +
                                " and a.so_no=b.so_no and b.so_no=c.so_no and b.so_line_no=c.so_line_no and c.sku=d.sku and " +
                                 " a.inv_org *= e.inv_org  and a.consignor *= e.consignor  and a.customer_sale_no *= e.work_no  " +
                                  "and a.inv_org='" + main.g_org + "' and a.consignor='" + str_consignor + "' and a.so_no='" + str_sono + "' order by c.so_line_no";
                    ds_tmp = main.dbservice.Sql_exec_selectsql(str_temp);
                }
                else  
                {
                    str_temp = "select case (select count(*) from " + main.g_space + ".so_pick_detail q where q.inv_org=a.inv_org and q.consignor=a.consignor and q.so_no=a.so_no) when 0 then '全部缺料' else '部分缺料' end short_dist,b.order_unit,a.consignor,a.so_no,a.so_day,c.so_line_no,c.wave_no,c.sku,d.sku_name,c.plan_so_qty_m,c.plan_so_qty_s" +
                               " ,c.short_qty_m,c.short_qty_s,a.customer_sale_no work_no,b.lot_attribute3 tax_dist,d.spec,d.type,e.work_line,b.lot_attribute5,d.warehouseman " +
                               " from " + main.g_space + ".so_ship_header a," + main.g_space + ".so_ship_detail b," + main.g_space + ".so_pick_short c," + main.g_space + ".mst_sku d," + main.g_space + ".so_work_no e where a.inv_org=b.inv_org and " +
                               " b.inv_org=c.inv_org and c.inv_org=d.inv_org and a.consignor=b.consignor and b.consignor=c.consignor and c.consignor=d.consignor " +
                                " and a.so_no=b.so_no and b.so_no=c.so_no and b.so_line_no=c.so_line_no and c.sku=d.sku and " +
                                 " a.inv_org = e.inv_org (+) and a.consignor = e.consignor (+) and a.customer_sale_no = e.work_no (+) " +
                                  "and a.inv_org='" + main.g_org + "' and a.consignor='" + str_consignor + "' and a.so_no='" + str_sono + "' order by c.so_line_no";
                    ds_tmp = main.dbservice.oracle_exec_selectsql(str_temp);
                }               
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get Information of bill's detail", main.g_info_dataset) + "!" + ee.ToString());

                return;
            }

            this.so_012_r1.SetDataSource(ds_tmp.Tables[0]);
            crv_so.ReportSource = so_012_r1;
        }

        private void f_displayreport_list( string str_sono)
        {
            DataSet ds_tmp;
            string str_temp;

            //得到拣货明细信息

            try
            {
                if (main.dbtype == "SQL")
                {
                    str_temp = "select case (select count(*) from " + main.g_space + ".so_pick_detail q where q.inv_org=a.inv_org and q.consignor=a.consignor and q.so_no=a.so_no) when 0 then '全部缺料' else '部分缺料' end short_dist,b.order_unit,a.consignor,a.so_no,a.so_day,c.so_line_no,c.wave_no,c.sku,d.sku_name,c.plan_so_qty_m,c.plan_so_qty_s" +
                               " ,c.short_qty_m,c.short_qty_s,a.customer_sale_no work_no,b.lot_attribute3 tax_dist,d.spec,d.type,e.work_line,b.lot_attribute5,d.warehouseman " +
                               " from " + main.g_space + ".so_ship_header a," + main.g_space + ".so_ship_detail b," + main.g_space + ".so_pick_short c," + main.g_space + ".mst_sku d," + main.g_space + ".so_work_no e where a.inv_org=b.inv_org and " +
                               " b.inv_org=c.inv_org and c.inv_org=d.inv_org and a.consignor=b.consignor and b.consignor=c.consignor and c.consignor=d.consignor " +
                                " and a.so_no=b.so_no and b.so_no=c.so_no and b.so_line_no=c.so_line_no and c.sku=d.sku and " +
                                 " a.inv_org *= e.inv_org  and a.consignor *= e.consignor  and a.customer_sale_no *= e.work_no  " +
                                  "and a.inv_org='" + main.g_org + "' and  a.so_no in( " + str_sono + ") order by a.so_no,c.so_line_no";
                    ds_tmp = main.dbservice.Sql_exec_selectsql(str_temp);
                }
                else
                {
                    str_temp = "select case (select count(*) from " + main.g_space + ".so_pick_detail q where q.inv_org=a.inv_org and q.consignor=a.consignor and q.so_no=a.so_no) when 0 then '全部缺料' else '部分缺料' end short_dist,b.order_unit,a.consignor,a.so_no,a.so_day,c.so_line_no,c.wave_no,c.sku,d.sku_name,c.plan_so_qty_m,c.plan_so_qty_s" +
                               " ,c.short_qty_m,c.short_qty_s,a.customer_sale_no work_no,b.lot_attribute3 tax_dist,d.spec,d.type,e.work_line,b.lot_attribute5,d.warehouseman " +
                               " from " + main.g_space + ".so_ship_header a," + main.g_space + ".so_ship_detail b," + main.g_space + ".so_pick_short c," + main.g_space + ".mst_sku d," + main.g_space + ".so_work_no e where a.inv_org=b.inv_org and " +
                               " b.inv_org=c.inv_org and c.inv_org=d.inv_org and a.consignor=b.consignor and b.consignor=c.consignor and c.consignor=d.consignor " +
                                " and a.so_no=b.so_no and b.so_no=c.so_no and b.so_line_no=c.so_line_no and c.sku=d.sku and " +
                                 " a.inv_org = e.inv_org (+) and a.consignor = e.consignor (+) and a.customer_sale_no = e.work_no (+) " +
                                  "and a.inv_org='" + main.g_org + "'  and a.so_no in (" + str_sono + ") order by a.so_no,c.so_line_no";
                    ds_tmp = main.dbservice.oracle_exec_selectsql(str_temp);
                }
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get Information of bill's detail", main.g_info_dataset) + "!" + ee.ToString());

                return;
            }

            this.so_012_r1.SetDataSource(ds_tmp.Tables[0]);
            crv_so.ReportSource = so_012_r1;
        }

        #endregion
        private void w_so012_Load(object sender, EventArgs e)
        {
            string str_sql;
            //初始化变量
            dscustomer = new DataSet();
            DataSet ds_asnname;
            string str_asnname = "";
            //取报表表头名称
            if (main.dbtype == "SQL")
                str_sql = " select Ltrim(Rtrim(id_data)) as id_data from " + main.g_space + ".sys_parameter where inv_org = '" + main.g_org + "' and id =1001 ";
            else
                str_sql = " select trim(id_data) as id_data from " + main.g_space + ".sys_parameter where inv_org = '" + main.g_org + "' and id =1001 ";
            try
            {
                if (main.dbtype == "SQL")
                    ds_asnname = main.dbservice.Sql_exec_selectsql(str_sql);
                else
                    ds_asnname = main.dbservice.oracle_exec_selectsql(str_sql);
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get Information of bill's detail", main.g_info_dataset) + "!" + ee.ToString());

                return;
            }
            if (ds_asnname.Tables[0].Rows.Count > 0)
            {
                str_asnname = ds_asnname.Tables[0].Rows[0][0].ToString().Trim();
            }
            //初始化货主编码 
            try
            {
                dscustomer = main.g_class.comm_get_consignor();

                cbx_customer.ValueMember = "consignor";
                cbx_customer.DisplayMember = "consignor";
                DataRow drnew;
                drnew = dscustomer.Tables[0].NewRow();
                drnew["consignor"] = "";
                drnew["consignor_name"] = "";
                dscustomer.Tables[0].Rows.Add(drnew);
                cbx_customer.DataSource = dscustomer.Tables[0];
                if (dscustomer.Tables[0].Rows.Count > 0) tb_cusname.Text = dscustomer.Tables[0].Rows[0][1].ToString().Trim();
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get Consignor's Information", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }
            //初始化波茨号
            try
            {
                string s_sql = "";
                DataSet ds_temp;
                if (main.dbtype == "SQL")
                {
                    s_sql = "select rtrim(ltrim(wave_no)) wave_no from " + main.g_space + ".so_ship_wave where inv_org='" + main.g_org + "' and wave_status='1' order by wave_no";
                    ds_temp = main.dbservice.Sql_exec_selectsql(s_sql);
                }
                else
                {
                    s_sql = "select trim(wave_no) wave_no from " + main.g_space + ".so_ship_wave where inv_org='" + main.g_org + "' and wave_status='1' order by wave_no";
                    ds_temp = main.dbservice.oracle_exec_selectsql(s_sql);
                }
                DataRow drnew;
                drnew = ds_temp.Tables[0].NewRow();
                drnew["wave_no"] = "";
                ds_temp.Tables[0].Rows.Add(drnew);
                cbx_wave_no.DataSource = ds_temp.Tables[0];
                cbx_wave_no.ValueMember = "wave_no";
                cbx_wave_no.DisplayMember = "wave_no";
                cbx_wave_no.Text = "";
            }
            catch (System.Exception ee)
            {
                MessageBox.Show("获取波次号失败!" + ee.ToString());
                return;
            }
            //日期控件默认从今天到一个星期
            //dtp_start.CustomFormat = "yyyy.MM.dd HH:mm:ss";
            dtp_start.CustomFormat = "yyyy.MM.dd";
            dtp_start.Format = DateTimePickerFormat.Custom;
            dtp_end.CustomFormat = "yyyy.MM.dd";
            dtp_end.Format = DateTimePickerFormat.Custom;
            dtp_start.Value = System.Convert.ToDateTime(DateTime.Now.ToString("yyyy.MM.dd").Trim());
            dtp_end.Value = System.Convert.ToDateTime(DateTime.Now.AddDays(7).ToString("yyyy.MM.dd").Trim());

            //初始化之后，改变语言
            //语言转换
            main.g_class.comm_changeControl(this, main.g_win_dataset);

            this.so_012_r1.Load("..//..//so012_r.rpt");
            TextObject text;
            text = so_012_r1.ReportDefinition.ReportObjects["tb_asnname"] as TextObject;
            text.Text = str_asnname;

            text = so_012_r1.ReportDefinition.ReportObjects["so_no"] as TextObject;
            text.Text = main.g_class.comm_getchangestring("so_no", main.g_win_dataset);
            text = so_012_r1.ReportDefinition.ReportObjects["Batch"] as TextObject;
            text.Text = main.g_class.comm_getchangestring("Batch", main.g_win_dataset);

            text = so_012_r1.ReportDefinition.ReportObjects["line_no"] as TextObject;
            text.Text = main.g_class.comm_getchangestring("line_no", main.g_win_dataset);

            text = so_012_r1.ReportDefinition.ReportObjects["so_day"] as TextObject;
            text.Text = main.g_class.comm_getchangestring("so_day", main.g_win_dataset);

            text = so_012_r1.ReportDefinition.ReportObjects["sku"] as TextObject;
            text.Text = main.g_class.comm_getchangestring("sku", main.g_win_dataset);

            text = so_012_r1.ReportDefinition.ReportObjects["sku_name"] as TextObject;
            text.Text = main.g_class.comm_getchangestring("sku_name", main.g_win_dataset);

            text = so_012_r1.ReportDefinition.ReportObjects["spec"] as TextObject;
            text.Text = main.g_class.comm_getchangestring("spec", main.g_win_dataset);

            text = so_012_r1.ReportDefinition.ReportObjects["plan_so_qty_m"] as TextObject;
            text.Text = main.g_class.comm_getchangestring("plan_pick_qty_m", main.g_win_dataset);

            text = so_012_r1.ReportDefinition.ReportObjects["plan_so_qty_s"] as TextObject;
            text.Text = main.g_class.comm_getchangestring("plan_pick_qty_s", main.g_win_dataset);

            text = so_012_r1.ReportDefinition.ReportObjects["short_qty_m"] as TextObject;
            text.Text = main.g_class.comm_getchangestring("short_qty_m", main.g_win_dataset);

            text = so_012_r1.ReportDefinition.ReportObjects["short_qty_s"] as TextObject;
            text.Text = main.g_class.comm_getchangestring("short_qty_s", main.g_win_dataset);

            text = so_012_r1.ReportDefinition.ReportObjects["tax_dist"] as TextObject;
            text.Text = main.g_class.comm_getchangestring("tax_dist", main.g_win_dataset);

            text = so_012_r1.ReportDefinition.ReportObjects["sum"] as TextObject;
            text.Text = main.g_class.comm_getchangestring("sum", main.g_win_dataset);

            text = so_012_r1.ReportDefinition.ReportObjects["order_unit"] as TextObject;
            text.Text = main.g_class.comm_getchangestring("order_unit", main.g_win_dataset);

            text = so_012_r1.ReportDefinition.ReportObjects["work_no"] as TextObject;
            text.Text = main.g_class.comm_getchangestring("work_no", main.g_win_dataset);

            //获取按钮权限
            string str_right;
            str_right = main.g_class.comm_getright("w_so012", main.g_right_dataset);
            //新增
            if (str_right.Substring(5, 1) == "0")
            {
                btn_all.Visible = false;
                btn_Reset.Visible = false;
                button1.Visible = false;
            }
            else
            {
                btn_all.Visible = true;
                btn_Reset.Visible = true;
                button1.Visible = true;
            }
        }

        private void cbx_customer_TextChanged(object sender, EventArgs e)
        {
            string str_cust;
            str_cust = cbx_customer.Text.Trim();
            tb_cusname.Text = main.g_class.comm_findname(str_cust, dscustomer);
        }

        private void btn_query_Click(object sender, EventArgs e)
        {
            string str_sql = "";
            DataSet ds_query;

            str_sql = "select 0 dist,a.consignor,a.so_no,a.customer_sale_no work_no,case a.so_type when '0' then '" + main.g_class.comm_getchangestring("Outbound", main.g_info_dataset) + "' " +
                         "when '1' then '" + main.g_class.comm_getchangestring("Return", main.g_info_dataset) + "' when '2' then '" + main.g_class.comm_getchangestring("Consignment ", main.g_info_dataset) + "' when '3' then '" +
                          main.g_class.comm_getchangestring("Appropriation ", main.g_info_dataset) + "' when '4' then '" + main.g_class.comm_getchangestring("Sale  ", main.g_info_dataset) + "' when '5' then '" +
                          main.g_class.comm_getchangestring("Other ", main.g_info_dataset) + "'  when '6' then '" + main.g_class.comm_getchangestring("delegate", main.g_info_dataset) + "'  when '7' then '" + main.g_class.comm_getchangestring("outsourcing", main.g_info_dataset) + "' when '8' then '" +
                          main.g_class.comm_getchangestring("guarantees", main.g_info_dataset) + "' when '9' then '" + main.g_class.comm_getchangestring("Consignment Move", main.g_info_dataset) + "' else '' end as so_type,so_day from " + main.g_space + ".so_ship_header a where a.inv_org='" + main.g_org + "'";

            //判断是否有货主
            if (cbx_customer.Text.Trim() != "")
            {
                str_sql = str_sql + " and a.consignor = '" + cbx_customer.Text.Trim() + "' ";
            }
            else //没有货主提取有权限的所有货主
            {
                str_sql = str_sql + " and a.consignor in (select a.consignor from " + main.g_space + ".mst_customer a," + main.g_space + ".sys_user b," + main.g_space + ".sys_user_customer c" +
                           " where b.inv_org = c.inv_org and b.user_id = c.user_id and c.inv_org = a.inv_org and c.consignor = a.consignor and b.inv_org = '" + main.g_org + "' and b.user_id = '" + main.g_user + "' and a.consignor_flag='1')  ";
            }
            //出库单号
            if (tb_so_no.Text.Trim() != "")
            {
                str_sql += " and a.so_no = '" + tb_so_no.Text.Trim() + "'";
            }
            //工作令号
            if (tb_work_no.Text.Trim() != "")
            {
                str_sql += " and a.customer_sale_no = '" + tb_work_no.Text.Trim() + "'";
            }
            //仓库管理员
            if (tb_warehouseman.Text.Trim() != "")
            {
                str_sql += " and exists (select 1 from " + main.g_space + ".so_pick_short b," + main.g_space + ".mst_sku c where a.inv_org=b.inv_org and b.inv_org=c.inv_org and a.consignor=b.consignor " +
                            "and b.consignor=c.consignor and a.so_no=b.so_no and b.sku=c.sku and b.short_qty_m>0 and c.warehouseman = '" + tb_warehouseman.Text.Trim() + "')";
            }
            //波次号
            if (cbx_wave_no.Text.Trim() != "")
            {
                str_sql += " and exists (select 1 from " + main.g_space + ".so_pick_short b where a.inv_org=b.inv_org and a.consignor=b.consignor " +
                            " and a.so_no=b.so_no  and b.short_qty_m>0 and b.wave_no like '%" + cbx_wave_no.Text.Trim() + "%')";
            }
            //只找出有缺料的单据
            str_sql += " and exists (select 1 from " + main.g_space + ".so_pick_short b where a.inv_org=b.inv_org and a.consignor=b.consignor " +
                          " and a.so_no=b.so_no  and b.short_qty_m>0 )";
            //加上日期
            str_sql = str_sql + " and a.so_day between '" + dtp_start.Text.ToString().Trim() + "' and '" + dtp_end.Text.ToString().Trim() + "' order by a.so_no ";
            //提取数据
            try
            {
                if (main.dbtype == "SQL")
                    ds_query = main.dbservice.Sql_exec_selectsql(str_sql);
                else
                    ds_query = main.dbservice.oracle_exec_selectsql(str_sql);
                //加载数据
                dg_so.DataSource = ds_query.Tables[0];
                if (ds_query.Tables[0].Rows.Count > 0) //说明有记录，默认第一条
                {
                    string str_storer = ds_query.Tables[0].Rows[0][0].ToString().Trim();
                    string str_sono = ds_query.Tables[0].Rows[0][1].ToString().Trim();
                    //显示表头信息，包括明细的列表
                   // f_displayreport(str_storer, str_sono);
                }
                else
                {
                     f_displayreport("", "");
                }
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get bill's Information", main.g_info_dataset) + "!" + ee.ToString());
            }

        }

        private void dg_so_SelectionChanged(object sender, EventArgs e)
        {
           
        }

      

        private void dg_so_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dg_so.CurrentRow != null)
            {
                if (dg_so.Rows[dg_so.CurrentRow.Index].Cells["dist"].Value.ToString().Trim() == "1")
                {
                    dg_so.Rows[dg_so.CurrentRow.Index].Cells["dist"].Value = 0;
                }
                else
                {
                    dg_so.Rows[dg_so.CurrentRow.Index].Cells["dist"].Value = 1;
                }

              
                f_displayreport(dg_so.Rows[dg_so.CurrentRow.Index].Cells["consignor"].EditedFormattedValue.ToString().Trim(), dg_so.Rows[dg_so.CurrentRow.Index].Cells["so_no"].EditedFormattedValue.ToString().Trim());
                
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dg_so.Rows.Count > 0)
            {
                string str_sono = "";
                for (int i = 0; i < dg_so.Rows.Count; i++)
                {
                    if (dg_so.Rows[i].Cells["dist"].Value.ToString().Trim() == "1")
                    {
                        if (str_sono.Length == 0)
                        {
                            str_sono += "'" + dg_so.Rows[i].Cells["so_no"].Value.ToString().Trim() + "'";
                        }
                        else
                        {
                            str_sono += ",'" + dg_so.Rows[i].Cells["so_no"].Value.ToString().Trim() + "'";
                        }
                    }
                }
                f_displayreport_list(str_sono);
                crv_so.PrintReport();
            }
        }

        private void btn_all_Click(object sender, EventArgs e)
        {
            if (dg_so.Rows.Count > 0)
            {
                for (int i = 0; i < dg_so.Rows.Count; i++)
                {
                    dg_so.Rows[i].Cells["dist"].Value = 1;
                }
            }
        }

        private void btn_Reset_Click(object sender, EventArgs e)
        {
            if (dg_so.Rows.Count > 0)
            {
                for (int i = 0; i < dg_so.Rows.Count; i++)
                {
                    if (dg_so.Rows[i].Cells["dist"].Value.ToString().Trim() == "1")
                    {
                        dg_so.Rows[i].Cells["dist"].Value = 0;
                    }
                    else
                    {
                        dg_so.Rows[i].Cells["dist"].Value = 1;
                    }
                }
            }
        }
    }
}